var statusLists = [
    {
        id: '-1',//-1 全部 0=暂存 10=审核中 1=审核通过 2=审核拒绝 3=已上架 4=已下架
        text: '全部'
    }, {
        id: '6',
        text: '已售罄'
    }, {
        id: '4',
        text: '已上架'
    }, {
        id: '5',
        text: '已下架'
    }, {
        id: '0',
        text: '暂存'
    }, {
        id: '1',
        text: '审核中'
    }, {
        id: '3',
        text: '审核拒绝'
    }, {
        id: '2',
        text: '审核通过'
    }];
var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        orderlist: [],
        statusList: this.statusLists,
        getdata_timeout: null,
        statuscount: [],
        productList: [],
        reload: false,
        // orderlist: [],
        // statusList:[],
        orderCount: 0,
        status: '-1',
        Keywords: '',
        selectId: 0,
        pageindex: 0,
        pagesize: 10,
        is_searching:false,
        can_scroll:false,
        current_item_length: 0,
    },
    methods: {
        // 链接到编辑
        handEdit(ID) {
            var that=this;
            that.selectId = 0
            ns.openWin('product_add', '编辑商品', { ID });

        },
        // 删除商品
        handDelete: function (ID,PrdName) {
            var that = this
            api.confirm({
                title: '提醒',
                msg: `确定删除【${PrdName}】商品?`,
                buttons: ['确定','取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    const params = {
                        action: 'deletebusinessproduct',
                        ID,
                    }
                    ns.post(params, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.productList = that.productList.filter(res=> Number(res.ID) !== Number(ID))
                        } else {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
            that.selectId = 0
        },
        //提交审核
        handAuditUp: function (ID,PrdName) {
            var that = this
            api.confirm({
                title: '提醒',
                msg: `确定将【${PrdName}】商品提交审核?`,
                buttons: ['确定','取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    const params = {
                        action: 'pushauditbusinessproduct',
                        ID,
                    }
                    ns.post(params, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.productList = that.productList.map(res=> {
                                if(Number(res.ID) === Number(ID)) res.Status = 1
                                return res
                            })
                            that.asyncProductList()
                        } else {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
            that.selectId = 0
        },
        //上架
        handUp: function (ID,PrdName) {
            var that = this
            api.confirm({
                title: '提醒',
                msg: `确定将【${PrdName}】商品上架?`,
                buttons: ['确定','取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    const params = {
                        action: 'upbusinessproduct',
                        ID,
                    }
                    ns.post(params, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.productList = that.productList.map(res=> {
                                if(Number(res.ID) === Number(ID)) res.Status = 4
                                return res
                            })
                            that.asyncProductList()
                        } else {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
            that.selectId = 0
        },
        // 下架
        handDown: function (ID,PrdName) {
            var that = this
            api.confirm({
                title: '提醒',
                msg: `确定将【${PrdName}】商品下架?`,
                buttons: ['确定','取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    const params = {
                        action: 'downbusinessproduct',
                        ID,
                    }
                    ns.post(params, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.productList = that.productList.map(res=> {
                                if(Number(res.ID) === Number(ID)) res.Status = 5
                                return res
                            })
                            that.asyncProductList()
                        } else {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
            that.selectId = 0
        },
        // 取消上架审核
        handUpCancel: function (ID,PrdName) {
            var that = this
            api.confirm({
                title: '提醒',
                msg: `确定取消【${PrdName}】商品审核?`,
                buttons: ['确定','取消']
            }, function (ret, err) {
                if (ret.buttonIndex == 1) {
                    const params = {
                        action: 'rejectpushauditbusinessproduct',
                        ID,
                    }
                    ns.post(params, function (succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                                that.productList = that.productList.map(res=> {
                                    if(Number(res.ID) === Number(ID)) res.Status = 0
                                    return res
                                })
                                that.asyncProductList()
                        } else {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
            that.selectId = 0
        },
        // 切换状态后整理数据
        asyncProductList(){
            const CheckCate = this.status
            if(Number(CheckCate) > -1){
                this.productList = this.productList.filter(res=> Number(res.Status) === Number(CheckCate))
            }
        },
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            // that.productList = [];
            var option = {};
            option.pageindex = that.pageindex;
            option.pagesize = that.pagesize;
            if (that.reload) {
                if (that.productList.length > 0) {
                    option.pageindex = 0;
                    option.pagesize = that.productList.length;
                }
                else {
                    that.pageindex = 0;
                    option.pagesize = that.pagesize;
                    option.pageindex = that.pageindex;
                }
                that.reload = false;
            }
            option.Keywords = that.Keywords;
            option.Status = that.status;
            option.action = 'selectproductlist';
            ns.post(option, function (succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    if (data.productList.length == that.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.pageindex == 0) {
                        that.productList = data.productList;
                    } else {
                        that.productList = that.productList.concat(data.productList);
                    }
                    that.current_item_length = that.productList.length;


                    // that.productList = data.productList;
                } else {
                    that.productList = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            });


        },
        do_openmenu: function (id) {
            var that = this;
            that.selectId = id;
        },
        closebox: function () {
            var that = this;
            that.selectId = 0;
        },
        onSelect(action) {
            Toast(action.text);
        },
        //导航栏切换
        randomSwitchBtn: function (name, id, index) {
            var that = this;
            that.pageindex=0;
            //调用接口给数据赋值
            that.status = id;
            that.orderCount = "";
            // that.total.ProcessCount = "";
            that.get_data();
        },
        // 商品详情
        do_opendetail: function (id) {
            var that = this;
            ns.openWin('product_detail', '商品详情', {
                id: id,
                searchtype: 30
            });
        },
        // 商品新增
        do_addProduct: function () {
            var that = this;
            ns.openWin('product_add', '新增商品');
        },
        // 调整库存
        updatenum: function (item) {
            var that = this;
            that.selectId = 0;
            ns.openWin('product_update_num', '调整库存', {
                item:item,
                headerbg:2
            });
        },

        //订单关闭
        set_status_bar: function () {
            var that = this;
            api.setStatusBarStyle({
                style: 'dark',
                color: 'transparent'
            });
        },
        // 链接到详情
        link2Detail:function(ID){
            ns.openWin('product_detail', '商品详情', {
                id:ID
            });
        }
    },
});

apiready = function () {
    api.parseTapmode();
    app.set_status_bar();
    ns = window.Foresight.Util;
    ns.init_pull();
    api.addEventListener({
        name: 'reloadlist'
    }, function (ret, err) {
        app.reload = true;
        app.get_data();
    });
    app.get_data();
    // api.addEventListener({
    //     name: 'onlogin'
    // }, function(ret, err) {
    //     app.get_data();
    // });
    // api.addEventListener({
    //     name: 'shopmsgevent'
    // }, function(ret, err){
    //       app.get_data();
    // });
    api.addEventListener({
        name: 'do_open_search'
    }, function (ret, err) {
        ns.openWin('search_frm', '搜索',{
            currentType:'product'
        });
    });
    api.addEventListener({
        name: 'do_open_fenlei'
    }, function (ret, err) {
        ns.openWin('product_sort', '商品分类');
    });

}
